import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // 登录页
    {path:'/login',component:()=> import('@/views/login/LoginPage.vue')},
    //布局页面
    {
      path:'/',
      component:()=> import('@/views/layout/LayoutContainer.vue'),
      redirect:'/article/manage',
      // 二级子路由
      children:[
        {path:'/article/manage',component:()=> import('@/views/article/ArticleManage.vue')},
        {path:'/article/channel',component:()=> import('@/views/article/ArticleChannel.vue')},
        // 用户页面
        // 头像
        {path:'/user/avatar',component:()=> import('@/views/user/UserAvatar.vue')},
        // 密码
        {path:'/user/password',component:()=> import('@/views/user/UserPassword.vue')},
        // 详情
        {path:'/user/profile',component:()=> import('@/views/user/UserProfile.vue')}
      ]
    },
  ],
})
// 未登录拦截
router.beforeEach((to, from) =>{
  // ...
  // 返回 false 以取消导航
  const userStore = useUserStore()
  if(!userStore.token && to.path!== '/login'){
    return '/login'
  }else{
    return from.pa
  }

})
export default router
